package com.shanxi.cz.module.biz.controller.admin.ammetermqtt;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.shanxi.cz.framework.common.pojo.PageParam;
import com.shanxi.cz.framework.common.pojo.PageResult;
import com.shanxi.cz.framework.common.pojo.CommonResult;
import com.shanxi.cz.framework.common.util.object.BeanUtils;
import static com.shanxi.cz.framework.common.pojo.CommonResult.success;

import com.shanxi.cz.framework.excel.core.util.ExcelUtils;

import com.shanxi.cz.framework.apilog.core.annotation.ApiAccessLog;
import static com.shanxi.cz.framework.apilog.core.enums.OperateTypeEnum.*;

import com.shanxi.cz.module.biz.controller.admin.ammetermqtt.vo.*;
import com.shanxi.cz.module.biz.dal.dataobject.ammetermqtt.AmmeterMqttDO;
import com.shanxi.cz.module.biz.service.ammetermqtt.AmmeterMqttService;

@Tag(name = "管理后台 - mq消息存储")
@RestController
@RequestMapping("/biz/ammeter-mqtt")
@Validated
public class AmmeterMqttController {

    @Resource
    private AmmeterMqttService ammeterMqttService;

    @PostMapping("/create")
    @Operation(summary = "创建mq消息存储")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-mqtt:create')")
    public CommonResult<Integer> createAmmeterMqtt(@Valid @RequestBody AmmeterMqttSaveReqVO createReqVO) {
        return success(ammeterMqttService.createAmmeterMqtt(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新mq消息存储")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-mqtt:update')")
    public CommonResult<Boolean> updateAmmeterMqtt(@Valid @RequestBody AmmeterMqttSaveReqVO updateReqVO) {
        ammeterMqttService.updateAmmeterMqtt(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除mq消息存储")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('biz:ammeter-mqtt:delete')")
    public CommonResult<Boolean> deleteAmmeterMqtt(@RequestParam("id") Integer id) {
        ammeterMqttService.deleteAmmeterMqtt(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除mq消息存储")
                @PreAuthorize("@ss.hasPermission('biz:ammeter-mqtt:delete')")
    public CommonResult<Boolean> deleteAmmeterMqttList(@RequestParam("ids") List<Integer> ids) {
        ammeterMqttService.deleteAmmeterMqttListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得mq消息存储")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-mqtt:query')")
    public CommonResult<AmmeterMqttRespVO> getAmmeterMqtt(@RequestParam("id") Integer id) {
        AmmeterMqttDO ammeterMqtt = ammeterMqttService.getAmmeterMqtt(id);
        return success(BeanUtils.toBean(ammeterMqtt, AmmeterMqttRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得mq消息存储分页")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-mqtt:query')")
    public CommonResult<PageResult<AmmeterMqttRespVO>> getAmmeterMqttPage(@Valid AmmeterMqttPageReqVO pageReqVO) {
        PageResult<AmmeterMqttDO> pageResult = ammeterMqttService.getAmmeterMqttPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AmmeterMqttRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出mq消息存储 Excel")
    @PreAuthorize("@ss.hasPermission('biz:ammeter-mqtt:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAmmeterMqttExcel(@Valid AmmeterMqttPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AmmeterMqttDO> list = ammeterMqttService.getAmmeterMqttPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "mq消息存储.xls", "数据", AmmeterMqttRespVO.class,
                        BeanUtils.toBean(list, AmmeterMqttRespVO.class));
    }

}